package com.zr.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zr.model.User;
import com.zr.service.LoginServicecy;
import com.zr.serviceImpl.LoginServiceImplcy;

public class LoginControllercy extends HttpServlet{
	//创建用户对象
	LoginServicecy  us = new LoginServiceImplcy();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	doPost(req, resp);
}
@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	//创建session对象
	HttpSession session =req.getSession();
	//创建返回值对象
 	PrintWriter pr=resp.getWriter();
 	//从页面获取用户名与密码值
 	String uname=req.getParameter("uname");
 	String upsw=req.getParameter("upsw");
 	String ck=req.getParameter("ck");
 	if("y".equals(ck)){
		 Cookie nameCookie = new Cookie("username", uname);
		 nameCookie.setMaxAge(60 * 60 * 24 * 3);
		Cookie pswCookie = new Cookie("password", upsw);
		 pswCookie.setMaxAge(60 * 60 * 24 * 3);
		  resp.addCookie(nameCookie);
		  resp.addCookie(pswCookie);
	}
 	User i =us.LoginUser(uname, upsw);
 	//判断是否用户是否存在
 	if(i.getUid()>0){
 		//在session中保存该用户的信息
 		session.setAttribute("User",i);
 		pr.write("1");
 	}else{
 		pr.write("0");
 	}
 	
	}
}
